//
//  LDSSliderHeadViewController.m
//  TestCategory
//
//  Created by kai_leedarson on 2017/11/6.
//Copyright © 2017年 maple_leedarson. All rights reserved.
//

#import "LDSSliderHeadViewController.h"
#import "LDSSliderHeadViewControllerViewModel.h"
#import "LDSCollectionViewModel.h"
#import "UIResponder+Router.h"

@interface LDSSliderHeadViewController ()

@property(nonatomic, strong) LDSSliderHeadViewControllerViewModel *viewControllerViewModel;//必须子类化
@property(nonatomic, strong) LDSCollectionViewModel *collectionViewModel;//选择性子类化
@property(nonatomic, strong) UICollectionViewFlowLayout *collectionLayout;
@property(nonatomic, strong) NSMutableArray *dataSource;
@property(nonatomic, strong) UIView *sliderBlockView;//滑块条
@end

@implementation LDSSliderHeadViewController

- (UIView *)sliderBlockView {
    if(_sliderBlockView == nil) {
        _sliderBlockView = [[UIView alloc] initWithFrame:CGRectMake(0,self.frame.size.height - 10, self.frame.size.width /4, 10)];
        _sliderBlockView.backgroundColor = [UIColor redColor];
    }
    return _sliderBlockView;
}

#pragma mark - lazy init
- (LDSSliderHeadViewControllerViewModel *)viewControllerViewModel {
    if (_viewControllerViewModel== nil) {
        _viewControllerViewModel = [[LDSSliderHeadViewControllerViewModel alloc] init];
        _viewControllerViewModel.delegate = (id<LDSViewControllerViewModelDelegate>) self;
    }
    return _viewControllerViewModel;
}

- (LDSCollectionViewModel *)collectionViewModel {
    if (_collectionViewModel == nil) {
        _collectionViewModel = [[LDSCollectionViewModel alloc] init];
        _collectionViewModel.delegate = (id<LDSCollectionViewModelDelegate>)self;
        //接管collectionView
        [_collectionViewModel handleWithCollection:self.collectionView];
    }
    return _collectionViewModel;
}

- (UICollectionView *)collectionView {
    if (_collectionView == nil) {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)
                                             collectionViewLayout:self.collectionLayout];
        //_collectionView.backgroundColor = RGB(235, 241, 241) ;
        _collectionView.backgroundColor = [UIColor cyanColor];
        [self addSubview:_collectionView];
    }
    return _collectionView;
}

- (UICollectionViewLayout *)collectionLayout {
    if (_collectionLayout == nil) {
        _collectionLayout = [[UICollectionViewFlowLayout alloc] init];
        [_collectionViewModel handleWithCollectionLayout:_collectionLayout];
        CGFloat photoSizeW = self.frame.size.width/4;
        CGFloat photoSizeH = self.frame.size.height;
        [_collectionLayout setItemSize:(CGSize){photoSizeW, photoSizeH}];
        [_collectionLayout setMinimumLineSpacing:12.0f];
        [_collectionLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    }
    return _collectionLayout;
}

- (NSMutableArray *)dataSource {
    if (_dataSource == nil) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}


- (NSString *)barItemTitle {
    return @"";
}

#pragma mark - life cycle
- (instancetype)init {
    if (self = [super init]) {
        [self viewSetUp];
        [self frameSetUp];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self viewSetUp];
        [self frameSetUp];
    }
    return self;
}

- (void)createData:(NSArray *)data {
    if (data.count > 0) {
        self.dataSource = [data mutableCopy];
        [self viewSetUp];
        
    }
}

- (void)viewSetUp {
    if (self.dataSource.count > 0 ) {
        [self.viewControllerViewModel loadFetchDataWithArray:self.dataSource];
    } else {
        [self.viewControllerViewModel loadFetchData];
    }
    [self addSubview:self.sliderBlockView];
}

- (void)frameSetUp {
    
}


#pragma mark - LDSCollectionViewModelDelegate
- (void)lds_collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [self routerEventWithName:@"headCellSelected" userInfo:@{@"indexPath":indexPath}];
    [UIView animateWithDuration:0.5 animations:^{
        self.sliderBlockView.frame = CGRectMake(self.frame.size.width /4 *(indexPath.row),self.frame.size.height - 10, self.frame.size.width /4, 10);
    }];
}

//- (UIEdgeInsets)lds_collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
//    return UIEdgeInsetsMake(14, 14,14, 14);
//}

#pragma mark - SecondVCViewModelDelegate
- (void)lds_didReceiveDataWithDataSource:(NSArray *)dataSource andSectionDataSource:(NSArray *)sectionDataSource {
    self.dataSource = [dataSource mutableCopy];
    [self.collectionViewModel handleWithDataSource:self.dataSource];
    [self.collectionView reloadData];
}
@end
